提示框、跟随气泡、Popconfirm、Dialog 和 Drawer 看起来都能“浮在页面上”,所以产品里最常见的错误不是把它们做得不好看,而是从一开始就选错了类型。

浮层借走的是用户的注意力。它越强,用户越难继续手头的工作。选择哪种浮层,本质上是在回答:当前任务需要多小的打断、保留多少上下文,以及用户是否能从错误里退回来。

浮层不是装饰,而是一次注意力借贷

选择浮层前,先看四件事:

  • 它是在解释信息,还是承接操作;
  • 影响的是一个元素、一个区域,还是一批对象;
  • 操作能否撤销;
  • 用户是否需要一边完成当前任务,一边查看页面上的其他内容。

如果一层界面为了完成简单动作却要求用户离开上下文,它就是过重的。反过来,真正不可逆的批量操作如果只用一个贴着按钮的小气泡确认,也会显得轻率。

视觉样式可以沿用设计系统,真正需要重新决定的是类型与行为。

先分辨:解释信息,还是承接操作

Tooltip:只为单个元素补充说明

Tooltip 适合被截断的长文本、纯图标按钮和需要解释的禁用控件。它只承担文字说明,不包含按钮、链接或任何可点击内容。

行为上,它应该在悬停与键盘聚焦时出现。第一个 Tooltip 可以等待约 500ms,让用户先表达意图;从一个图标移到相邻图标时,再立即显示下一个,避免每次都要重新等待。

Tooltip 需要始终留在视口内,设置 pointer-events: none,同一时间只显示一个,并与触发元素通过 aria-describedby 关联。提示本身不能挡住用户正在查看或操作的内容。

如果提示里需要放操作,它就不再是 Tooltip,而应该改成点击打开的 Popover。

Popover:单个元素上的轻量操作

Popover 适合从某个按钮或元素展开一组短操作。它和 Tooltip 的关键区别不在于长得多像气泡,而在于交互入口:Tooltip 由悬停或聚焦触发,Popover 由点击打开,并允许用户在其中操作。

Popover 应当锚定在触发元素旁,点击外部或按 Esc 可以关闭,关闭后焦点回到打开它的元素。它适合轻量、上下文明确、不会影响整页结构的操作。

跟随提示:服务的是一个连续区域

图表、地图和其他连续画布上的提示不应该固定挂在某一个点上。

跟随指针的提示在指针进入区域时出现,跟着指针移动,离开区域后消失。它要避开指针本身,并在接近视口边缘时翻转方向。图表上还可以同时显示引导线,把当前值吸附到最近的数据点。

这类提示服务的是“指针在一个区域内移动时持续读数”,而不是“解释指针下方那个固定元素”。两者的信息范围和锚点都不同。

能撤销,就不要先确认

确认框不是安全感的来源,可恢复性才是。

如果操作可以撤销,就立即执行,再提供明确的恢复入口。比如删除后显示带「撤销」的 toast,或者保留一个能撤回操作的回收站入口。

这样用户不用为每个可控动作停一次,产品也不会把确认框当成免责声明。确认应该只出现在用户真的无法从错误中恢复的时候。

因此,顺序应该是:

1
2
能撤销 → 直接执行 + Undo
不可撤销 → 再根据影响范围选择确认方式

不可逆操作再按影响范围升级

不可逆操作也不意味着全部上 Dialog。

如果一个操作只影响用户自己的单个对象,而且触发按钮就在附近,使用 Popconfirm。它贴着按钮打开,路径短,也不会打断页面里的其他工作。

Popconfirm 应该从触发按钮附近生长出来;按 Esc 或点击外部可以关闭,焦点返回原按钮。确认按钮必须写明动作,例如「删除文件」,而不是只写「确定」。

如果操作影响很大、会影响其他人、涉及批量对象,或者用户还必须先做另一个决定,就应该升级成 Dialog。它要求用户明确停下来,确认对象、后果和下一步。

Dialog 要说明谁或什么会受到影响,以及哪些结果不可撤销。使用系统既有的模态组件,或者用原生 <dialog> 的 showModal() 建立真正的模态语义;焦点留在内部,Esc 取消,不要只靠遮罩层模拟。

表单的容器由任务长度和上下文决定

表单放错容器,用户会在“是否看得见背景信息”和“是否被强制打断”之间反复挣扎。

短表单:Dialog

字段少、填写时不需要参考页面上的其他内容,适合放进 Dialog。用户完成、取消或按 Esc 后回到原来的位置,任务边界清楚。

长表单:Drawer

字段多、需要一边填写一边查看背后的页面,或者横向空间明显不足时,使用 Drawer。它从依附的边缘进入,页面仍然可见,适合把编辑面板当作当前页面的延伸。

Drawer 不适合用来确认一个操作。它的职责是编辑和处理,不是让用户停下来做决定。

如果编辑结果需要实时反映在背后的页面里,Drawer 很合适;如果用户需要的是独立、专注的流程,就应该改用页面。

主任务:独立页面

当表单本身就是用户来到这里的主要任务,或者结果需要独立 URL、可分享、刷新后仍能恢复时,使用页面。

Dialog 和 Drawer 都是当前任务的辅助层,页面才是任务本身。不要把一个完整工作流塞进浮层,再通过多层返回把人困住。

无论使用 Dialog、Drawer 还是页面,只要表单有未保存修改,关闭前都应该先询问。

触摸屏与键盘会重新定义“可发现”

触摸屏没有悬停。任何只能在 hover 时出现的信息,都必须提供点击显示的入口。

在桌面端,Tooltip 需要同时响应鼠标悬停和键盘聚焦;在移动端,点击可以打开等价的说明或 Popover。用户点击外部、按 Esc 或完成动作后,浮层要可靠关闭,焦点回到触发位置。

一次只展示一个浮层。Dialog 要保持模态焦点,Drawer 要管理进入与返回的焦点,Popover 和 Popconfirm 要支持外部点击与 Esc。浮层出现、消失的动效也要尊重 prefers-reduced-motion,但不能因为减少动效就失去操作路径。

用一张表完成初步判断

场景 默认选择 关键条件
为单个元素补充只读说明 Tooltip 悬停或聚焦触发,不含操作
图表、地图等连续区域读数 跟随提示 指针进入区域后持续更新
单个元素上的轻量操作 Popover 点击打开,可承载操作
单个对象的不可逆操作 Popconfirm 就地确认,影响范围小
高影响、批量或影响他人的操作 Dialog 用户必须停下并理解后果
短表单,无需参照页面 Dialog 字段少,任务边界明确
长表单,需要对照页面或宽度 Drawer 保留上下文,便于横向编辑
主任务、需要独立 URL 或刷新保留 页面 可分享、可恢复、可独立完成

判断后仍要回到设计系统:使用已有组件、颜色、圆角、阴影、字体、动效和文案风格。这里解决的是类型错误,不是重新发明一套浮层视觉。

检查清单

  • Tooltip 是否只包含说明文字,操作是否放进 Popover;
  • Tooltip 是否支持悬停、键盘聚焦、视口避让和 aria-describedby;
  • 跟随提示是否服务于连续区域,而不是固定元素;
  • 可撤销操作是否直接执行并提供 Undo;
  • 单对象不可逆操作是否使用就地 Popconfirm;
  • 高影响、批量或影响他人的操作是否升级为 Dialog;
  • 短表单、长表单与主任务是否分别使用 Dialog、Drawer 和页面;
  • Dialog 与 Drawer 是否正确管理焦点、Esc、关闭与未保存修改;
  • 触摸屏是否可以点击到达所有 hover 信息;
  • 是否先交付决策表,再修改代码。

浮层选择示例 Prompt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Prompt

Review how my product uses floating UI (tooltips, tooltips that follow the pointer, popovers, popconfirms, drawers and dialogs) and decide which kind each case should be. Work inside my existing design system. This is about choosing the right kind, not about restyling: keep my components, colours, radii, shadows, fonts, motion and copy style.

0. Start from what exists
- Find every overlay in the product and the components behind it: tooltips, hover cards, popovers, confirm bubbles, menus that confirm, side panels, drawers, sheets, modals and dialogs.
- For each use, note where it is, what opens it, what it holds and what happens when the user confirms.
- Find the design system's own component for each kind. Reuse it. If two components do the same job, say so and suggest which one to keep.

1. What each kind is for
- Tooltip: adds text about what the pointer is on, such as long text that has been cut short, an icon-only button or a disabled control. Text only. If it needs actions, it becomes a popover opened by a click.
- Tooltip that follows the pointer: keeps giving information across an area. It appears when the pointer enters, follows it, and goes when the pointer leaves. Common on charts and on other self-contained elements.
- Popconfirm: the second check for an irreversible action on a single item, right where it was clicked. The path stays short and nothing else is interrupted.
- Dialog: for something the user must stop and confirm or complete before going on: an action with a large impact, one that affects other people, a bulk action, or one that needs another decision first (such as who takes over the projects). Also for a short form that doesn't need anything else on the page for reference.
- Drawer: for a long form, for work that needs the page in view to compare against, or to make better use of horizontal space. Never use a drawer to confirm something.
- Full page: when the task is the user's main job there, or it needs a link that can be shared and survives a reload.

2. Questions that settle a case
- Can the action be undone? Then don't confirm: do it at once and offer Undo (a toast, or a way back such as a trash).
- If it can't: is it one item of the user's own, with a button to anchor to? Popconfirm. Large impact, other people, a bulk selection, or a decision still to make? Dialog.
- Is it a form? Short, and nothing on the page is needed for reference: dialog. Long, needs the page in view, or wants the width: drawer. The main task, or needs its own URL: a page.
- Is the information about one element, or about wherever the pointer is inside an area? One element: tooltip. An area: tooltip that follows the pointer. Either way, does it need anything to click? Then it is a popover.
- On touch screens there is no hover: anything shown on hover must also be reachable by a tap.

3. What each kind should do
Fill in only what is missing, with the design system's existing parts.
- Tooltip: shows on hover and on keyboard focus, after about 500ms for the first one, then at once when moving to a neighbour. Stays inside the viewport, lets the pointer through, one at a time, linked with aria-describedby.
- Tooltip that follows the pointer: sits beside the pointer without covering it and flips near the edges of the viewport. On a chart, the guide line follows the pointer and the value snaps to the nearest data point. On touch, a tap shows the value.
- Popconfirm: anchored to the button that opened it and grows from it. Escape or a click outside closes it, and focus goes back to that button. The confirm button names the action.
- Dialog: the system's modal or a native dialog opened with showModal(). Keeps focus inside and Escape cancels. It says who or what is affected and what cannot be undone. Buttons name the action ("Remove member", not "OK").
- Drawer: comes in from the edge it is attached to and leaves the page in view. Changes show up in the page behind it as they are made. Closes with Escape and a close button; focus moves into it and back out to what opened it.
- Drawers, dialogs and pages with a form: closing with unsaved changes asks first.

4. Keep the design as it is
- Change only the kind where the current one is wrong, plus the missing behaviour from step 3.
- Don't change colours, radii, shadows, fonts, spacing or animations, and don't add new visual patterns.
- Keep copy as it is, except confirm buttons and the line that explains what cannot be undone.
- If a case needs a component the system doesn't have (an undo toast, a popover), stop and ask me before adding one.

5. Deliver the decisions first
- Before changing any code, give me a table: each use, where it is, its current kind, the kind you recommend, and why in one line.
- Mark the ones that are already right, and the ones that could fit two kinds, with what decides between them.
- Wait for my go-ahead, then make only the changes I approve and list every file you touched.

质感来自可解释的细节

浮层选择的答案不在视觉层,而在因果层。

信息只属于一个元素时,用 Tooltip;需要在区域内连续读数时,让提示跟随指针;能撤销时直接执行;必须中断时,再按影响范围选择 Popconfirm 或 Dialog;表单则根据长度、上下文和是否拥有独立 URL 落进 Dialog、Drawer 或页面。

每一次打断都能被解释,产品的交互才会显得轻,而不是显得少。

站内搜索

没有找到内容!